/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaServices;

//import mapper.MembersMapper;
import entities.Members;
import entities.MyBatisUtil;
import java.util.List;
import mapper.MembersMapper;
import org.apache.ibatis.session.SqlSession;
import services.ConnexionBD;

/**
 *
 * @author michael
 */
public class MemberService{
    
    
    
    public static void insertUser(Members user) {
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  MembersMapper userMapper = sqlSession.getMapper(MembersMapper.class);
	  userMapper.insertUser(user);
	  sqlSession.commit();
	  }finally{
	   sqlSession.close();
	  }
	 }
	 
	 public static Members getUserById(Integer userId) {
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  MembersMapper userMapper = sqlSession.getMapper(MembersMapper.class);
	  return userMapper.getUserById(userId);
	  }finally{
	   sqlSession.close();
	  }
	 }
	 
         public static Members getUserByEmail(String Username) {           
             
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  MembersMapper userMapper = sqlSession.getMapper(MembersMapper.class);
	  return userMapper.getUserByEmail(Username);
	  }finally{
	   sqlSession.close();
	  }
	 }
         
	 public static List<Members> getAllUsers() {
	  SqlSession sqlSession = ConnexionBD.getConnexion();
          List<Members> maliste;
	  try{
//	  MembersMapper userMapper = sqlSession.getMapper(MembersMapper.class);
//	  return userMapper.getAllUsers();
               maliste= sqlSession.selectList("myBatis.MemberMapper.getAll");
	  }finally{
              
	   sqlSession.close();
	  }
          return maliste;
         }
	 
	 public static void updateUser(Members user) {
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  MembersMapper userMapper = sqlSession.getMapper(MembersMapper.class);
	  userMapper.updateUser(user);
	  sqlSession.commit();
	  }finally{
	   sqlSession.close();
	  }
	   
	 }
	 
	 public static void deleteUser(Integer userId) {
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  MembersMapper userMapper = sqlSession.getMapper(MembersMapper.class);
	  userMapper.deleteUser(userId);
	  sqlSession.commit();
	  }finally{
	   sqlSession.close();
	  }
         }
//         
//         public List<Members> searchByName(String firstNameMembers)
//         {
//             SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
//             try{
//                   MembersMapper userMapper = sqlSession.getMapper(MembersMapper.class);
//                   return  userMapper.searchByName(firstNameMembers);
//           }finally{
//	   sqlSession.close();
//	}
//      }
         
//  public List<Members> searchByAge(int AgeMembers)
//         {
//             SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
//             try{
//                   MembersMapper userMapper = sqlSession.getMapper(MembersMapper.class);
//                   return  userMapper.searchByAge(AgeMembers);
//           }finally{
//	   sqlSession.close();
//	}
//      }
        
//           public List<Members> searchByAgeBetween18_25()
//         {
//             SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
//             try{
//                   MembersMapper userMapper = sqlSession.getMapper(MembersMapper.class);
//                   return  userMapper.searchByAgeBetween18_25();
//           }finally{
//	   sqlSession.close();
//	}
//      }
//           
//           public List<Members> searchByAgeBetween25_35()
//         {
//             SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
//             try{
//                   MembersMapper userMapper = sqlSession.getMapper(MembersMapper.class);
//                   return  userMapper.searchByAgeBetween25_35();
//           }finally{
//	   sqlSession.close();
//	}
//      }
//           
//           public List<Members> searchByAgeBetween35_45()
//         {
//             SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
//             try{
//                   MembersMapper userMapper = sqlSession.getMapper(MembersMapper.class);
//                   return  userMapper.searchByAgeBetween35_45();
//           }finally{
//	   sqlSession.close();
//	}
//      }
//           public List<Members> searchByAgeBetween45_55()
//         {
//             SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
//             try{
//                   MembersMapper userMapper = sqlSession.getMapper(MembersMapper.class);
//                   return  userMapper.searchByAgeBetween45_55();
//           }finally{
//	   sqlSession.close();
//	}
      
           
}





